//
// Created by liushuai on 2020/3/23.
//
#include "isValidBST.h"
int* last = nullptr;
bool isValidBST(TreeNode* root)
{
    if (root)
    {
        if (!isValidBST(root->left))
            return false;
        if (last && *last >= root->val)
            return false;
        last = &root->val;
        return isValidBST(root->right);
    }
    else
        return true;
}
bool isValidBST1(TreeNode* root, long min, long max)
{
    if (!root)
        return true;
    if (root->val > min && root->val < max)
        return isValidBST1(root->left, min, root->val) && isValidBST1(root->right, root->val, max);
    else
        return false;
}
bool isValidBST1(TreeNode* root)
{
    return isValidBST1(root, (numeric_limits<long>::min)(), (numeric_limits<long>::max)());
}
